<?php
set_time_limit(0);
error_reporting(15);
require_once("header.php");
require_once("functions.php");

$num = @$_REQUEST['num'];
$keywords = @$_REQUEST['keywords'];

$howmany = array('All',5,10,25,50,75,100);
?>
<form action="index.php" method="post" name="keyword_suggestions">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100" align="left" valign="top" class="bodytext"><strong>Keyterm:</strong></td>
    <td><input type="text" name="keywords" value="<?php echo $keywords; ?>" size="50" /><br><span class="footer">Number of suggestions to display</span></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="left" valign="top" class="bodytext"><strong>Results:</strong></td>
    <td><select name="num">
	<?php
	foreach($howmany as $number ) {
		if ($number == $num) {
			echo '<option value="'.$number.'" selected>'.$number.' Suggestions</option>';
		} else {
			echo '<option value="'.$number.'">'.$number.' Suggestions</option>';
		}
	}
	?></select><br><span class="footer">Number of suggestions to display</span> 
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input name="Analyze" type="submit" id="Analyze" value="Analyze" class="button" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
<?php
if (isset($_REQUEST['Analyze']) && isset($_REQUEST['keywords']) )
{
	//echo "<pre>";print_r($keywords);die;
	
	$htmlcode =  file_get_contents("http://inventory.overture.com/d/searchinventory/suggestion/?mkt=us&lang=en_US&term=".urlencode($keywords));
	
	if ($htmlcode === false ) {
		$error_text = 'Server is busy. Please try again later';
	} else {
	
		$p = strpos($keywords, '<body');
		$text = substr($htmlcode, $p);
		
		$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
		$text = preg_replace('@<styles[^>]*?>.*?</styles>@si', '', $text);
		$text = preg_replace('@<form[^>]*?>.*?</form>@si', '', $text);
		
		$mytds = get_html_tags($text,'td');
		
		$results = array();
		$j = 0;
		//print_r($mytds);
		
		for($i=0; $i<count($mytds); )
		{
			$results[$j][0] = html_entity_decode(strip_tags($mytds[$i++]['text']));
			$results[$j++][1] = html_entity_decode(strip_tags($mytds[$i++]['text']));
		}
		
		if (count($results) == 0 ) {
			$error_text = 'No suggestions for <b>'.$keywords.'</b>';
		}
	}
if (!empty($error_text)) {
	echo '<p class="quotecontent" align="center"><br><br>'.$error_text.'<br><br></p>';
} else {
?>
<table width="460" border="0" cellspacing="0" cellpadding="0" class="mytable">
  <tr>
    <td height="25"><strong>Results for: <?php echo $keywords; ?></strong> </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td align="center" valign="top"><table width="400" border="0" cellpadding="0" cellspacing="0" bgcolor="#000000">
      <tr>
        <td><table width="400" border="0" cellspacing="1" cellpadding="0">
          <tr class="bodytext" bgcolor="#FDFDFD">
            <td width="206" height="25" align="center"><strong>Term</strong></td>
            <td width="194" align="center"><strong>Searches this month</strong></td>
          </tr>
		  <?php
		  $t = 0;
		  $f = 0;
		  
		  if($num > count($results) || $num == 'All' )
		  	$f = count($results);
		  else
		  	$f = $num;

		  for($i=0; $i<$f; $i++)
		  {
		  ?>
          <tr height="22" class="tr<?php echo (($t % 2 == 0)?'1':'2'); ?>">
            <td><?php echo $results[$i][1]; ?></td>
            <td style="padding-left:4px;"><?php echo $results[$i][0]; ?></td>
          </tr>
		  <?php
		  $t++;
		  }
		  ?>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
</table>
<?php
	}
}

require_once("footer.php");
?>